package core;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.ImageIcon;

/**
 * @author Arno de Jager
 *
 */
public class Map implements Drawable {
	private ArrayList<ArrayList<Point2D.Double>> routes;
	private ImageIcon background = new ImageIcon("images/background.gif");
	private Base base;
	private	ArrayList<Polygon> polygonArray = new ArrayList<Polygon>();
	private int ID;
	/**
	 * Breedte van de weg
	 */
	public static final int wayWidth = 35;

	public Map( Base base, ArrayList<ArrayList<Point2D.Double>> routes, int ID )
	{
		this.base = base;
		this.routes = routes;
		this.ID = ID;
	}
	
	public int getID(){
		return ID;
	}

	/**
	 * @return the routes
	 */
	public ArrayList<ArrayList<Point2D.Double>> getRoutes() {
		return routes;
	}

	/**
	 * @return the background
	 */
	public ImageIcon getBackground() {
		return background;
	}

	/**
	 * @return the base
	 */
	public Base getBase() {
		return base;
	}

	/**
	 * Teken achtergrond en routes
	 * @see core.Drawable#draw(java.awt.Graphics, java.awt.Rectangle, java.awt.Component)
	 */
	public void draw(Graphics g, Rectangle area, Component c) {
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		// Teken achtergrond
		g.drawImage( this.background.getImage(), 0, 0, area.width, area.height, c );
		
		// Teken routes
		for( ArrayList<Point2D.Double> route : routes ) {
			
			// Teken punten
			Point2D.Double prevPoint = null;
			for( Point2D.Double p : route ) {
				
				// We tekenen de route per 2 punten
				if ( prevPoint != null )
				{	
					g2.setColor( new Color(107, 130, 139) );
					
					Polygon polygon = new Polygon();
					polygon.addPoint((int)prevPoint.x, (int)prevPoint.y);
					polygon.addPoint((int)prevPoint.x, (int)(prevPoint.y + wayWidth));
					polygon.addPoint((int)p.x, (int)(p.y + wayWidth));
					polygon.addPoint((int)p.x, (int)p.y);
					g2.fillPolygon( polygon );
					
					g2.setColor( Color.WHITE );
					g2.drawLine( (int)p.x, (int)p.y, (int)prevPoint.x, (int)prevPoint.y );
					g2.drawLine( (int)p.x, (int)(p.y + wayWidth), (int)prevPoint.x, (int)(prevPoint.y + wayWidth) );
					
					polygonArray.add(polygon);
				}
				prevPoint = p;
			}
		}
		
		// Teken base
		base.draw( g, area, c );
	}
	
	public ArrayList<Polygon> getPolygonArray(){
		return polygonArray;
	}

}
